
package com.nikmesoft.android.alwaysontime.activities;

import java.io.Serializable;
import java.util.ArrayList;
import com.nikmesoft.android.alwaysontime.R;
import com.nikmesoft.android.alwaysontime.adapters.TaskAdapter;
import com.nikmesoft.android.alwaysontime.daos.TasksDAO;
import com.nikmesoft.android.alwaysontime.models.SmsMainItem;
import com.nikmesoft.android.alwaysontime.models.Task;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;


public class TasksActivity extends BaseActivity  implements OnItemClickListener{
	Button btnAddTask;
	Spinner spnTask;
	ArrayAdapter<CharSequence> spnAdapter;	
	ListView lstTask;
	ArrayList<Task> taskList;
	TaskAdapter taskAdapter;
	// database
	TasksDAO tasksDAO;
	static final int TODAY_TASK = 0;
	static final int MONTH_TASK = 1;
	static final int ALL_TASK = 2;
	// private static final int REQUEST_CODE_TASK_ADD = 111;
	// private static final int REQUEST_CODE_TASK_VIEW = 222;


	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_tasks);
		View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.activity_tasks, null);
		this.setContentView(viewToLoad);
		init();
	}

	private void init()
	{
		// database va listview vao tao list 
		tasksDAO = TasksDAO.getInstance();
		lstTask = (ListView) findViewById(R.id.lstTask);
		taskList = new ArrayList<Task>();
		//lay data cho taskList
		taskList = tasksDAO.getTasks(TODAY_TASK);
		
		taskAdapter = new TaskAdapter(this, R.layout.task_list_item, taskList);
		lstTask.setAdapter(taskAdapter);
		lstTask.setOnItemClickListener(this);

		// nap du lieu cho spinner
		spnTask = (Spinner) findViewById(R.id.spnTask);
		spnAdapter = ArrayAdapter.createFromResource(this, R.array.activity_tasks_lsttask, 
				android.R.layout.simple_spinner_item);
		spnAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnTask.setAdapter(spnAdapter);		
		setSpinnerListenner();

		//add listener for do a task button
		addListenerForAddButton();
	}

	// ========= spinner =============
	public void setSpinnerListenner()
	{	spnTask.setOnItemSelectedListener(new OnItemSelectedListener()
		{	@Override
			public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
			{	//Log.i("Spinner click tai vi tri  : ", "id spinner = " + id);
				Log.i("Spinner click tai position  : ", "position spinner = " + position);
				switch (position)
				{	case 0:// today tasks
						taskList = tasksDAO.getTasks(TODAY_TASK);
						break;
					case 1://this month
						taskList = tasksDAO.getTasks(MONTH_TASK);
						break;
					case 2://all
						taskList = tasksDAO.getTasks(ALL_TASK);
						break;
						
				}
				reloadtaskList();
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{}
		});
	}
	public void reloadtaskList()
	{	taskAdapter.clear();
		for (int i = 0; i < taskList.size(); i++)
			taskAdapter.add(taskList.get(i));
		taskAdapter.notifyDataSetChanged();
	}

	//========= listenner for Add button
	public void addListenerForAddButton(){	
		btnAddTask = (Button) findViewById( R.id.btnAddTask);
		btnAddTask.setOnClickListener( new OnClickListener()
		{	public void onClick(View arg0)
			{	
				TasksGroup tasksparent = (TasksGroup)getParent();
				Intent intent = new Intent(tasksparent, TasksActivityAdd.class);
				intent.putExtra("AddOrEdit", "");
				tasksparent.startChildActivity(TasksActivityAdd.class.getSimpleName(), intent);
			}
		});
	}
	
	//click on item
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
		TasksGroup tasksparent = (TasksGroup)getParent();
		Intent intent = new Intent( tasksparent, TasksActivityAdd.class);
		Task item = taskAdapter.getItem(position);

		intent.putExtra("AddOrEdit", "edit");
		Bundle bundle = new Bundle();
		bundle.putSerializable("task",item);
		intent.putExtras(bundle);
		tasksparent.startChildActivity(TasksActivityAdd.class.getSimpleName(), intent);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){	
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100)
		{	if (resultCode == RESULT_OK)
			{	
			}
			else if (resultCode == RESULT_CANCELED)
			{	
			}
		}
	}	

}
